﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ResourceDll;
using System.IO;
using System.Diagnostics;

namespace BackupRestoreApplication.Develop
{
    public partial class frmTopstyle : Form
    {
        BackgroundWorker m_oWorker;
        public frmTopstyle()
        {
            InitializeComponent();
            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
        }
        #region BackgroundWorker

        void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                lbtxtStatus.Text = "Task Cancelled.";
            }
            else if (e.Error != null)
            {
                lbtxtStatus.Text = "Error while performing background operation.";
            }
            else
            {
                lbtxtStatus.Text = "Task Completed...";
            }
        }
        void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lbtxtStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }
        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(50);
                m_oWorker.ReportProgress(i);
                if (m_oWorker.CancellationPending)
                {
                    e.Cancel = true;
                    m_oWorker.ReportProgress(0);
                    return;
                }
            }
            m_oWorker.ReportProgress(100);
        }
        #endregion

        #region Button //////////
        private void btBackup_Click(object sender, EventArgs e)
        {
            try
            {
                string checkf1 = frmMain.path1 + @"\TopStyle 5\TopStyle5.exe";
                string checkf2 = frmMain.path2 + @"\TopStyle 5\TopStyle5.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "TopStyle5")
                            app.Kill();

                    this.btBackup.Enabled = false;
                    this.btRestore.Enabled = false;
                    this.btCancel.Enabled = false;
                    //_7zDll.Delete();
                    string source = _7zDll.pathroaming + @"\Bradsoft.com\TopStyle.\5.0\*";
                    string target = Application.StartupPath + @"\DBBACKUP\TopStyle5\TopStyle5.7z";
                    _7zDll.Compress(source, target);
                    m_oWorker.RunWorkerAsync();
                    lbtxtFinish.Text = "Job : Backup !";
                }
                else
                {
                    MessageBox.Show("TopStyle5 don't install!", "Error");
                }

                this.btBackup.Enabled = true;
                this.btRestore.Enabled = true;
                this.btCancel.Enabled = true;
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }

        private void btRestore_Click(object sender, EventArgs e)
        {
            try
            {
                string checkf1 = frmMain.path1 + @"\TopStyle 5\TopStyle5.exe";
                string checkf2 = frmMain.path2 + @"\TopStyle 5\TopStyle5.exe";
                if (File.Exists(checkf1) || File.Exists(checkf2))
                {
                    foreach (Process app in Process.GetProcesses())
                        if (app.ProcessName == "TopStyle5")
                            app.Kill();

                    this.btBackup.Enabled = false;
                    this.btRestore.Enabled = false;
                    this.btCancel.Enabled = false;
                    string source = Application.StartupPath + @"\DBBACKUP\TopStyle5\TopStyle5.7z";
                    string dest = _7zDll.pathroaming + @"\Bradsoft.com\TopStyle\5.0";
                    _7zDll.ExtractFile(source, dest);
                    m_oWorker.RunWorkerAsync();
                    lbtxtFinish.Text = "Job : Restore !";
                }
                else
                {
                    MessageBox.Show("TopStyle5 don't install!", "Error");
                }

                this.btBackup.Enabled = true;
                this.btRestore.Enabled = true;
                this.btCancel.Enabled = true;
            }
            catch
            {
                MessageBox.Show("Error! Please contact: caiwindao@outlook.com", "Error");
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        private void frmTopstyle_Load(object sender, EventArgs e)
        {
            this.pictureBox1.Image = ResourceDll.Img.topstyle;
        }
    }
}
